﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project.MVC.DataAccess;
using Webdiyer.WebControls.Mvc;
using Project.MVC.Admin.Models;
using Project.MVC.Admin.Util;
namespace Project.MVC.Admin.Controllers
{
    public class CategoryController : Controller
    {
        CategoryAccess _categoryAccess = new CategoryAccess();
        public ActionResult Index(int id=1,int type=1)
        {
            var model = new CategoryModel();
            model.Type = type;
            model.PageList = _categoryAccess.GetAllCategories(false, type).ToPagedList(id, 40);
            return View("Index",model);
        }
        public ActionResult Edit(int id)
        {
            var model = new CategoryModel();
            var category = _categoryAccess.GetByKey(id);
            model.Category = category;
            model.Type = category.Type;
            model.ParentCategories = _categoryAccess.GetAllCategories(false, category.Type).ToList();
            return View("Edit", model);
        }
        [HttpPost]
        public ActionResult Edit(FormCollection form)
        {
            var model = new CategoryModel();            
            var category = new Category();
            category.Id = int.Parse(form.Get("txtId"));
            category.Name = form.Get("txtName");
            if (form.Get("ddlParentCateId") != null && !string.IsNullOrEmpty(form.Get("ddlParentCateId")))
            {
                category.ParentCategoryId = int.Parse(form.Get("ddlParentCateId"));
            }
            else category.ParentCategoryId = 0;            
            category.DisplayOrder = int.Parse(form.Get("txtDisplayOrder"));
            category.UpdatedOnUtc = DateTime.Now;
            category.Deleted = false;
            _categoryAccess.Update(category);
            model.Category = category;
            model.ParentCategories = _categoryAccess.GetAllCategories(false, category.Type).ToList();
            model.Message = "Đã lưu dữ liệu thành công";
            return View("Edit", model);
        }
        public ActionResult Delete(int id,int type)
        {
            var model = new Category();
            model.Id = id;
            _categoryAccess.Delete(model);
            return RedirectToAction("Index", new { Controller = "Category", type = type });

        }
        public ActionResult Add(int type=1)
        {
            var model = new CategoryModel();
            model.Type = type;
            model.Category = new Category();
            model.ParentCategories = _categoryAccess.GetAllCategories(false,type).ToList();
            return View("Add", model);
        }
        [HttpPost]
        public ActionResult Add(int type, FormCollection form)
        {
            var model=new CategoryModel();
            model.Type = type;
            var category = new Category();
            category.Type = type;
            category.Name = form.Get("txtName");
            if (form.Get("ddlParentCateId") != null && !string.IsNullOrEmpty(form.Get("ddlParentCateId")))
            {
                category.ParentCategoryId = int.Parse(form.Get("ddlParentCateId"));
            }
            else category.ParentCategoryId = 0; 
            category.DisplayOrder = int.Parse(form.Get("txtDisplayOrder"));
            category.CreatedOnUtc = DateTime.Now;
            category.Deleted = false;
            _categoryAccess.Insert(category);
            model.Category = category;
            model.ParentCategories = _categoryAccess.GetAllCategories(false, type).ToList();
            model.Message = "Đã lưu dữ liệu thành công";
            return View("Edit", model);
        }
    }
}
